package com.cashmama.market.api.application.controller.picture;

import com.cashmama.market.api.application.controller.picture.response.OssAppTokenVO;
import com.cashmama.market.api.application.controller.picture.response.PictureResponseVO;
import com.cashmama.market.api.domain.oss.OSSTokenService;
import com.cashmama.market.api.domain.picture.PictureService;
import com.cashmama.market.api.domain.vo.RequestLocalInfo;
import com.cashmama.market.api.domain.vo.picture.OssAppToken;
import com.cashmama.market.api.infras.handler.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: market-api
 * @description
 * @author: frank
 * @create: 2019-12-27 09:59
 **/
@RestController
@RequestMapping("/picture")
public class OssController {

    @Autowired
    private OSSTokenService ossTokenService;
    @Autowired
    private PictureService pictureService;

    /**
     * 获取oss token
     * @return
     */
    @RequestMapping(value = "/oss/token/get", method = RequestMethod.POST)
    public ApiResponse<OssAppTokenVO> getOssToken() {

        OSSTokenService.OssAppTokenInfo ossAppTokenInfo = ossTokenService.assumeRole(RequestLocalInfo.getCurrentUser().getUserCode());
        OssAppToken ossAppToken = OssAppToken.translate(ossAppTokenInfo);
        return new ApiResponse<>(new OssAppTokenVO(ossAppToken));
    }

    /**
     * 上传图片
     * @param request
     * @return
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST, produces = {"application/json;charset=utf-8"})
    public ApiResponse<PictureResponseVO> uploadPicture(HttpServletRequest request) {
        String userCode = RequestLocalInfo.getCurrentUser().getUserCode();
        String url = pictureService.uploadPicture(request, userCode);
        PictureResponseVO pictureResponseVO = new PictureResponseVO(url);
        return new ApiResponse<>(pictureResponseVO).setMsg("upload success");
    }

}
